<?php
// @formatter:off
/**
 * @file c.comic.related.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-07-05 14:30:06 +0000 (Fri, 05 Jul 2013) $
 *
 * $Id: c.comic.related.php 89 2013-07-05 14:30:06Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.1/extensions/RelatedComics/c.comic.related.php $
 */
// @formatter:on

global $wcComic;

if($wcComic && $wcComic->ok() && $wcProfile->allowedTo(WC_PERM_COMIC_VIEW, WC_PERM_COMIC_LIST)) {
	require_once "{$this->_themeRoot}/controlers/generics/comicpreview.php";
	require_once "{$this->_themeRoot}/controlers/generics/pager.php";

	global $wcCollection;
	global $wctDefaults;
	global $wcComicsHolder;

	$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
	$minCmcInPage = $wctDefaults["max-per-page"]["tag"] * ($page - 1);
	$maxCmcInPage = $minCmcInPage + $wctDefaults["max-per-page"]["tag"];

	$wcThemeAssigns["PATH"] = "Comics related to {$wcComic->name}";

	$tags = $wcTagsHolder->tagsOf($wcComic, $wcGroup);
	//
	// Comic information.
	// @{
	$wcThemeAssigns["INFO-DIRECTORY"] = $wcComic->directory()->name;
	if($wcProfile->granted(WC_PERM_DIRECTORY_VIEW)) {
		$wcThemeAssigns["INFO-DIRECTORY-LINK"] = $wcComic->directory;
	}
	if($wcCollection) {
		$wcThemeAssigns["INFO-COLLECTIONS"] = $wcCollection->collectionPath();
	}
	$wcThemeAssigns["INFO-TAGS"] = array();
	foreach($tags as $tag) {
		$aux = array(
			"name" => $tag->name,
			"link" => $tag->id
		);

		if(in_array($tag->name, $wcSpecialTags)) {
			$aux["special"] = true;
		}

		$wcThemeAssigns["INFO-TAGS"][$tag->name] = $aux;
	}
	ksort($wcThemeAssigns["INFO-TAGS"]);
	// @}
	//
	// .
	$comicIds = array();
	foreach($tags as $tag) {
		foreach($tag->members() as $id) {
			@$comicIds[$id]++;
		}
	}
	if(isset($comicIds[$wcComic->id])) {
		unset($comicIds[$wcComic->id]);
	}
	arsort($comicIds);

	$wcThemeAssigns["comics"] = array();
	$comicCount = 0;
	$wcThemeAssigns["most-comics"] = array();
	$mostComicCount = 0;
	foreach($comicIds as $id => $count) {
		$comic = $wcComicsHolder->item($id, $wcGroup);

		if($comic && $comic->ok()) {
			if($comicCount >= $minCmcInPage && $comicCount < $maxCmcInPage) {
				$wcThemeAssigns["comics"][] = wcGetComicPreviewAssigns($comic);
			}
			if($mostComicCount < 5) {
				$wcThemeAssigns["most-comics"][] = wcGetComicPreviewAssigns($comic);
			}
			$comicCount++;
			$mostComicCount++;
		}
	}

	wcSetPager(requestParams(true, $wcAction, array('page')), ceil($comicCount / $wctDefaults["max-per-page"]["tag"]), $page);
} else {
	if(!$wcProfile->allowedTo(WC_PERM_COMIC_VIEW, WC_PERM_COMIC_LIST)) {
		$wcThemeAssigns["error-message"] = "You don't have enough priviliges";
		require "{$this->_themeRoot}/controlers/contents/c.403.php";
	} else {
		$wcThemeAssigns["error-message"] = "There seems to be a problem with parameters";
		require "{$this->_themeRoot}/controlers/contents/c.500.php";
	}
}
?>